package com.learn.code.juc.thread;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Queue;
import java.util.concurrent.*;

/**
 * @author : 翌擎科技
 * @date : 2022/8/2 14:23
 */
public class ThreadPoolDemo {

    static Queue<String> queue = new ConcurrentLinkedDeque<String>();

    static {
        for (int i = 0; i < 10; i++) {
            queue.add("task-" + i);
        }
    }

    /**
     * 定义线程池
     */

    ThreadFactory threadFactory = new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
            Thread thread = defaultThreadFactory.newThread(r);
            thread.setName("Thread_Pool");
            return thread;
        }
    };

    ThreadPoolExecutor threadPoolExecutor = new
            ThreadPoolExecutor(20, 50, 60,
            TimeUnit.SECONDS, new ArrayBlockingQueue<>(5000),
            r -> {
                ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
                Thread thread = defaultThreadFactory.newThread(r);
                thread.setName("Thread_Pool");
                return thread;
            },
            new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadFactory threadFactory = Executors.defaultThreadFactory();

        System.out.println("---------------------newSingleThreadExecutor-------------------------------------------");
        //单线程池
//        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
//        for (int i = 0; i < 10; i++) {
//            singleThreadExecutor.execute(()->{
//                System.out.println(Thread.currentThread().getName()+"正在执行");
//            });
//        }

//        System.out.println("---------------------newCachedThreadPool-------------------------------------------");
//        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
//        for (int i = 0; i < 10; i++) {
//            newCachedThreadPool.execute(() -> {
//                System.out.println(Thread.currentThread().getName() + "正在执行");
//            });
//        }
//
//        System.out.println("---------------------newFixedThreadPool-------------------------------------------");
//        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(10);
//        for (int i = 0; i < 100; i++) {
//            newFixedThreadPool.execute(() -> {
//                System.out.println(Thread.currentThread().getName() + "正在执行");
//            });
//        }

        System.out.println("---------------------newScheduledThreadPool-------------------------------------------");
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);

        /**
         * 一秒钟之后执行业务代码
         */
//        scheduledExecutorService.schedule(() -> {
//            System.out.println(Thread.currentThread().getName() + "正在执行");
//        }, 1, TimeUnit.SECONDS);


        for (String poll : queue) {
            ScheduledFuture<String> stringScheduledFuture = scheduledExecutorService.schedule(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    System.out.println("时间:" + LocalDateTime.now() + "线程" + Thread.currentThread().getName() + " 执行了task: " + poll);
                    return poll + "::" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
                }
            }, 1, TimeUnit.SECONDS);
            String s = stringScheduledFuture.get();
            System.out.println(s);
        }

        scheduledExecutorService.scheduleAtFixedRate(() -> {
            System.out.println(Thread.currentThread().getName() + "正在执行" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, 1, 1, TimeUnit.SECONDS);



//        scheduledExecutorService.scheduleWithFixedDelay(() -> {
//            System.out.println(Thread.currentThread().getName() + "正在执行"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")));
//            try {
//                Thread.sleep(1500);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }, 1, 1, TimeUnit.SECONDS);

    }
}
